//
//  YHStatusView.swift
//  YHWeibo
//
//  Created by 昱含 on 16/5/23.
//  Copyright © 2016年 YuHan. All rights reserved.
//

import UIKit
import SnapKit
let originalContentTextFont: CGFloat = 14

let StatusCellMargin: CGFloat = 8
class YHStatusView: UIView {
    
    var bottomCons: Constraint?
    
    var statusViewModel : YHStatusViewModel? {
        didSet{

            headIcon.sd_setImageWithURL(statusViewModel?.headImageURL)
            nameLabel.text = statusViewModel?.status?.user?.name
            timeLabel.text = statusViewModel?.time
            sourceLabel.text = statusViewModel?.sourceText
            contentLabel.text = statusViewModel?.status?.text
            verifiedImageView.image = statusViewModel?.verifiedImage
            mbrankImageView.image = statusViewModel?.mbrankImage
            
            //给配图视图设置数据源
//            pictureView.pictures = statusViewModel?.status?.pic_urls
            bottomCons?.uninstall()
            if let urls = statusViewModel?.status?.pic_urls where urls.count > 0{
                
                pictureView.pictures = urls
                
                self.snp_updateConstraints(closure: { (make) in
                   
                    self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                    
                })
                
                pictureView.hidden = false
                
            }else{
                
                pictureView.pictures = nil
                
                pictureView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) in
                    
                    self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                    
                })
                
            }
        
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
//        backgroundColor = UIColor.yellowColor()
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        seperateView.backgroundColor = UIColor.lightGrayColor()
        addSubview(seperateView)
        addSubview(headIcon)
        addSubview(verifiedImageView)
        addSubview(nameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //设置约束
        
        seperateView.snp_makeConstraints { (make) in
            make.top.left.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
        }
        
        headIcon.snp_makeConstraints { (make) in
            make.top.left.equalTo(seperateView.snp_bottom).offset(StatusCellMargin)
            make.height.equalTo(40)
            make.width.equalTo(40)
        }
        
        verifiedImageView.snp_makeConstraints { (make) in
            make.right.bottom.equalTo(headIcon)
        }
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headIcon.snp_top)
            make.left.equalTo(headIcon.snp_right).offset(StatusCellMargin)
        }
        
        mbrankImageView.snp_makeConstraints { (make) in
            make.top.equalTo(headIcon.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
        }
        timeLabel.snp_makeConstraints { (make) in
            make.left.equalTo(headIcon.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(headIcon.snp_bottom)
        }
        sourceLabel.snp_makeConstraints { (make) in
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(headIcon.snp_bottom)
        }
        
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headIcon.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(headIcon.snp_left)
            
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentLabel.snp_left)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        self.snp_makeConstraints { (make) in
            self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }

        
    }
    
    private lazy var seperateView: UIView = UIView()
    private lazy var headIcon: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //认证的头像
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    private lazy var timeLabel: UILabel = UILabel(title: "11:11", font: 10, textColor: UIColor.orangeColor())
    private lazy var sourceLabel: UILabel = UILabel(title: "王大锤", font: 10, textColor: UIColor.darkGrayColor())
    private lazy var nameLabel: UILabel = UILabel(title: "杜雷", font: 14, textColor: UIColor.darkGrayColor())
    private lazy var contentLabel: UILabel = UILabel(title: "我是王大锤,我为自己带盐,我是王大锤,我为自己带盐我是王大锤,我为自己带盐我是王大锤,我为自己带盐我是王大锤,我为自己带盐我是王大锤,我为自己带盐我是王大锤,我为自己带盐我是王大锤,我为自己带盐", font: 14, textColor: UIColor.darkGrayColor(), alignment: .Left)
    private lazy var pictureView: YHStatusPictureView = YHStatusPictureView()


}
